package com.dayuanit.dy17.mix.thread;

public class ThreadDead {
    public static void main(String[] args) throws InterruptedException {
        Runnable task = new Runnable(){
            @Override
            public void run() {
                while (true && !Thread.currentThread().isInterrupted()) {
                    System.out.println("run() -- " +  Thread.currentThread().getName());

                    try {
                        Thread.sleep(2 * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        //一旦抛出了中断异常，那么状态位就重置了，所以，需要重新发送一下中断信号。
//                        Thread.currentThread().interrupt();
//                        break;//或者直接break,跳出循环。
                    }
                }

                System.out.println(Thread.currentThread().getName() + " 响应中断，结束。");
            }
        };

        Thread t1 = new Thread(task, "t1");
        System.out.println(t1.getState());
        t1.start();
        System.out.println(t1.getState());

//        t1.join();

        Thread.sleep(5 * 1000);
        System.out.println("向T1发送中断信号");
        t1.interrupt();

        System.out.println(t1.getState());
        System.out.println("main over");

    }
}
